<?php
/**
 * @author yuanyitian
 * @date 2023/6/5 21:08
 */

namespace api\admin;

use utils\redis;

class account
{

    public function login($data)
    {
        $con=con();
        $username=$data["username"];
        $password=$data["password"];
        $res=mysqli_query($con,"SELECT a.id,b.id as user_id,b.name,b.phone,b.sex,b.is_vip,b.nickname,b.avatar FROM `account` a LEFT JOIN user b on (a.user_id=b.id) where a.`name`='$username' and a.`password`='$password' and a.`status`=1 and a.is_delete=1");
        $res=mysqli_fetch_all($res,true);
        mysqli_close($con);

        if(count($res)==0){
            return out_error("账户名或密码错误！");
        }else{
            $res=$res[0];
            $id=$res["id"];
            $token = MD5( $id.time() . md5($data["password"]).mt_rand());
            $redis=redis::con();
            $redis->setex($token,1800,$id);
            $redis->close();
            $res["token"] = $token;
            return out_right($res);
        }


    }

    public function logout($data){
        $redis=redis::con();
        $token_key=$_SERVER['HTTP_X_TOKEN'];
        $res=$redis->get($token_key);
        if(!empty($res)){
            $redis->del($token_key);
        }
        $redis->close();
        return out_right("退出登录成功！");
    }

    public function info($data){
        $con=con();
        $login_user_id=$data["login_user_id"];

        $account=mysqli_query($con,"SELECT a.id,b.id as user_id,b.name,b.phone,b.sex,b.is_vip,b.nickname,b.avatar FROM `account` a LEFT JOIN user b on (a.user_id=b.id) where a.id=$login_user_id and a.`status`=1 and a.is_delete=1");
        $account=mysqli_fetch_all($account,true);
        if(count($account)==0){
            mysqli_close($con);
            return out_error("无此账户！");
        }
        $account=$account[0];

//        $role=mysqli_query($con,"select * from account_role_config where account_id=$login_user_id");
//        $role=mysqli_fetch_all($role,true);
//        $role=array_column($role,"role_id");
//        $role=array_map("intval",$role);
//        $role_sql=implode(",",$role);
//
//        $power=mysqli_query($con,"select * from role_power_config where role_id in (".$role_sql.")");
//        $power=mysqli_fetch_all($power,true);
//        $power=array_column($power,"power_id");
//        $power=array_map("intval",$power);


        $res=[];
//        $res["power"]=$power;
//        $res["role"]=$role;
        $res["roles"]=["admin"];
        if($account['is_vip']==1){
            $res["introduction"]='会员用户';
        }else{
            $res["introduction"]='普通用户';
        }

        $res["avatar"]=$account['avatar'];
        $res["name"]=$account["name"];

        mysqli_close($con);
        return out_right($res);
    }
}